package com.example.robertluozizhao.framecollectdemo.view.custom;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ListView;

/**
 * Created by Lzz on 8/3/2019.
 */

public class VerticalInterceptListView extends ListView {

    private ViewGroup parent;//设置为ScrollView
    private float mLastY;
    private boolean isDownOrUpMove = true; //标识向下滑动 还是 向上滑动

    public VerticalInterceptListView(Context context) {
        this(context, null);
    }

    public VerticalInterceptListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean result = super.onTouchEvent(ev);
        switch (ev.getAction()) {

            case MotionEvent.ACTION_MOVE:
                //当 y轴往下偏移 偏移量>0 ;y轴往上偏移 偏移量<0
                isDownOrUpMove = (ev.getY() - mLastY) > 0;

                //当可见列表第一个item 下标为0，证明滑到顶部位置
                if (getFirstVisiblePosition() == 0) {
                    if (isDownOrUpMove) {
                        //当向下滑，ScrollView处理事件，整体内容向下滑动
                        //所以，申请ScrollView不拦截事件为false，即ScrollView将拦截Move事件
                        setRequestDisallowIntercept(false);
                        result = false;
                    } else {
                        //当向上滑，ListView处理事件，ListView内容往上滑动
                        result = true;
                    }
                } else if (getLastVisiblePosition() == getCount() - 1) {
                    //同理，证明滑到底部部位置
                    if (isDownOrUpMove) {
                        //当向下滑，ListView处理事件，ListView内容往下滑动
                        result = true;
                    } else {
                        //当向上滑，ScrollView处理事件，整体内容向上滑动
                        setRequestDisallowIntercept(false);
                        result = false;
                    }
                }
                break;
        }
        mLastY = ev.getY();
        return result;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //这里申请父容器ScrollView不拦截down事件，ScrollView默认自己处理了down事件，会导致子View获取不了
                //down事件，那么之后后续事件都将传递不到子View.
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }


    public void setParent(ViewGroup parent) {
        this.parent = parent;
    }


    public void setRequestDisallowIntercept(boolean disallowIntercept) {
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(disallowIntercept);
            requestLayout();//刷新重绘，让View重走测量，布局方法
        }
    }

    public boolean getDownOrUpMoveState() {
        return isDownOrUpMove;
    }
}
